namespace C1.Win.C1FlexGrid
{
    using C1.Win.C1FlexGrid.Util;
    using C1.Win.C1FlexGrid.Util.Styles;
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;

    public class CellStyle
    {
        internal Style _style;
        private static TextAlignMap[] _taMap = new TextAlignMap[] { new TextAlignMap(TextAlignEnum.LeftTop, AlignHorzEnum.Near, AlignVertEnum.Top), new TextAlignMap(TextAlignEnum.LeftCenter, AlignHorzEnum.Near, AlignVertEnum.Center), new TextAlignMap(TextAlignEnum.LeftBottom, AlignHorzEnum.Near, AlignVertEnum.Bottom), new TextAlignMap(TextAlignEnum.CenterTop, AlignHorzEnum.Center, AlignVertEnum.Top), new TextAlignMap(TextAlignEnum.CenterCenter, AlignHorzEnum.Center, AlignVertEnum.Center), new TextAlignMap(TextAlignEnum.CenterBottom, AlignHorzEnum.Center, AlignVertEnum.Bottom), new TextAlignMap(TextAlignEnum.RightTop, AlignHorzEnum.Far, AlignVertEnum.Top), new TextAlignMap(TextAlignEnum.RightCenter, AlignHorzEnum.Far, AlignVertEnum.Center), new TextAlignMap(TextAlignEnum.RightBottom, AlignHorzEnum.Far, AlignVertEnum.Bottom), new TextAlignMap(TextAlignEnum.GeneralTop, AlignHorzEnum.General, AlignVertEnum.Top), new TextAlignMap(TextAlignEnum.GeneralCenter, AlignHorzEnum.General, AlignVertEnum.Center), new TextAlignMap(TextAlignEnum.GeneralBottom, AlignHorzEnum.General, AlignVertEnum.Bottom) };

        internal CellStyle(Style style)
        {
            this._style = style;
        }

        public string BuildString()
        {
            string str = string.Empty;
            StyleElementFlags definedElements = this.DefinedElements;
            if ((definedElements & StyleElementFlags.Font) != StyleElementFlags.None)
            {
                str = str + string.Format("Font:{0};", Types.ToString(this.Font));
            }
            if ((definedElements & StyleElementFlags.BackColor) != StyleElementFlags.None)
            {
                str = str + string.Format("BackColor:{0};", Types.ToString(this.BackColor));
            }
            if ((definedElements & StyleElementFlags.ForeColor) != StyleElementFlags.None)
            {
                str = str + string.Format("ForeColor:{0};", Types.ToString(this.ForeColor));
            }
            if ((definedElements & StyleElementFlags.Margins) != StyleElementFlags.None)
            {
                str = str + string.Format("Margins:{0};", Types.ToString(this.Margins));
            }
            if ((definedElements & StyleElementFlags.TextAlign) != StyleElementFlags.None)
            {
                str = str + string.Format("TextAlign:{0};", Types.ToString(this.TextAlign));
            }
            if ((definedElements & StyleElementFlags.TextEffect) != StyleElementFlags.None)
            {
                str = str + string.Format("TextEffect:{0};", Types.ToString(this.TextEffect));
            }
            if ((definedElements & StyleElementFlags.ImageAlign) != StyleElementFlags.None)
            {
                str = str + string.Format("ImageAlign:{0};", Types.ToString(this.ImageAlign));
            }
            if ((definedElements & StyleElementFlags.ImageSpacing) != StyleElementFlags.None)
            {
                str = str + string.Format("ImageSpacing:{0};", Types.ToString(this.ImageSpacing));
            }
            if ((definedElements & StyleElementFlags.Trimming) != StyleElementFlags.None)
            {
                str = str + string.Format("Trimming:{0};", Types.ToString(this.Trimming));
            }
            if ((definedElements & StyleElementFlags.WordWrap) != StyleElementFlags.None)
            {
                str = str + string.Format("WordWrap:{0};", Types.ToString(this.WordWrap));
            }
            if ((definedElements & StyleElementFlags.Border) != StyleElementFlags.None)
            {
                CellBorder border = this.Border;
                str = str + string.Format("Border:{0},{1},{2},{3};", new object[] { Types.ToString(border.Style), border.Width, Types.ToString(border.Color), Types.ToString(border.Direction) });
            }
            return str;
        }

        public void Clear()
        {
            this.Clear(StyleElementFlags.All);
        }

        public void Clear(StyleElementFlags flags)
        {
            if ((flags & StyleElementFlags.Font) != StyleElementFlags.None)
            {
                this._style.RemoveAttribute("Font");
            }
            if ((flags & StyleElementFlags.BackColor) != StyleElementFlags.None)
            {
                this._style.RemoveAttribute("BackColor");
            }
            if ((flags & StyleElementFlags.ForeColor) != StyleElementFlags.None)
            {
                this._style.RemoveAttribute("ForeColor");
            }
            if ((flags & StyleElementFlags.Margins) != StyleElementFlags.None)
            {
                this._style.RemoveAttribute("Padding");
            }
            if ((flags & StyleElementFlags.Border) != StyleElementFlags.None)
            {
                this._style.RemoveAttribute("Border");
            }
            if ((flags & StyleElementFlags.TextEffect) != StyleElementFlags.None)
            {
                this._style.RemoveAttribute("TextEffect");
            }
            if ((flags & StyleElementFlags.ImageAlign) != StyleElementFlags.None)
            {
                this._style.RemoveAttribute("AlignImage");
            }
            if ((flags & StyleElementFlags.ImageSpacing) != StyleElementFlags.None)
            {
                this._style.RemoveAttribute("Spacing");
            }
            if ((flags & StyleElementFlags.Trimming) != StyleElementFlags.None)
            {
                this._style.RemoveAttribute("Trimming");
            }
            if ((flags & StyleElementFlags.WordWrap) != StyleElementFlags.None)
            {
                this._style.RemoveAttribute("Wrap");
            }
            if ((flags & StyleElementFlags.TextAlign) != StyleElementFlags.None)
            {
                this._style.RemoveAttribute("AlignHorz");
                this._style.RemoveAttribute("AlignVert");
            }
        }

        public override bool Equals(object o)
        {
            CellStyle style = o as CellStyle;
            return ((style != null) && (style._style == this._style));
        }

        private Color GetColor(string sAtt)
        {
            return ((SolidBrush) this._style.GetAttribute(sAtt)).Color;
        }

        internal Rectangle GetContentRectangle(Rectangle rc)
        {
            return this._style.GetContentRectangle(rc);
        }

        internal HorizontalAlignment GetEditorAlign(string s)
        {
            switch (((AlignHorzEnum) this._style.GetAttribute("AlignHorz")))
            {
                case AlignHorzEnum.General:
                    if (Types.IsNumeric(s))
                    {
                        return HorizontalAlignment.Right;
                    }
                    return HorizontalAlignment.Left;

                case AlignHorzEnum.Near:
                    return HorizontalAlignment.Left;

                case AlignHorzEnum.Center:
                    return HorizontalAlignment.Center;

                case AlignHorzEnum.Far:
                    return HorizontalAlignment.Right;
            }
            return HorizontalAlignment.Left;
        }

        public override int GetHashCode()
        {
            return this._style.GetHashCode();
        }

        internal string GetStyleElement(string str, string tag)
        {
            int index = str.IndexOf(tag + ":");
            if (index < 0)
            {
                return null;
            }
            index += tag.Length;
            int num2 = str.IndexOf(";", index);
            if (num2 < 0)
            {
                return null;
            }
            return str.Substring(index + 1, (num2 - index) - 1);
        }

        public bool ParseString(string str)
        {
            try
            {
                string styleElement = this.GetStyleElement(str, "Font");
                if (styleElement != null)
                {
                    this.Font = (System.Drawing.Font) Types.FromString(styleElement, typeof(System.Drawing.Font));
                }
                styleElement = this.GetStyleElement(str, "BackColor");
                if (styleElement != null)
                {
                    this.BackColor = (Color) Types.FromString(styleElement, typeof(Color));
                }
                styleElement = this.GetStyleElement(str, "ForeColor");
                if (styleElement != null)
                {
                    this.ForeColor = (Color) Types.FromString(styleElement, typeof(Color));
                }
                styleElement = this.GetStyleElement(str, "Margins");
                if (styleElement != null)
                {
                    this.Margins = (C1.Win.C1FlexGrid.Util.Margins) Types.FromString(styleElement, typeof(C1.Win.C1FlexGrid.Util.Margins));
                }
                styleElement = this.GetStyleElement(str, "ImageAlign");
                if (styleElement != null)
                {
                    this.ImageAlign = (ImageAlignEnum) Types.FromString(styleElement, typeof(ImageAlignEnum));
                }
                styleElement = this.GetStyleElement(str, "ImageSpacing");
                if (styleElement != null)
                {
                    this.ImageSpacing = (int) Types.FromString(styleElement, typeof(int));
                }
                styleElement = this.GetStyleElement(str, "TextEffect");
                if (styleElement != null)
                {
                    this.TextEffect = (C1.Win.C1FlexGrid.TextEffectEnum) Types.FromString(styleElement, typeof(C1.Win.C1FlexGrid.TextEffectEnum));
                }
                styleElement = this.GetStyleElement(str, "TextAlign");
                if (styleElement != null)
                {
                    this.TextAlign = (TextAlignEnum) Types.FromString(styleElement, typeof(TextAlignEnum));
                }
                styleElement = this.GetStyleElement(str, "Trimming");
                if (styleElement != null)
                {
                    this.Trimming = (StringTrimming) Types.FromString(styleElement, typeof(StringTrimming));
                }
                styleElement = this.GetStyleElement(str, "Wrap");
                if (styleElement != null)
                {
                    this.WordWrap = (bool) Types.FromString(styleElement, typeof(bool));
                }
                styleElement = this.GetStyleElement(str, "Border");
                if (styleElement != null)
                {
                    string[] strArray = styleElement.Split(new char[] { ',' });
                    string data = string.Empty;
                    if (strArray.Length == 4)
                    {
                        data = strArray[2];
                    }
                    if (strArray.Length == 6)
                    {
                        data = strArray[2] + "," + strArray[3] + "," + strArray[4];
                    }
                    CellBorder border = this.Border;
                    border.Style = (BorderStyleEnum) Types.FromString(strArray[0], typeof(BorderStyleEnum));
                    border.Width = (int) Types.FromString(strArray[1], typeof(int));
                    border.Color = (Color) Types.FromString(data, typeof(Color));
                    border.Direction = (BorderDirEnum) Types.FromString(strArray[strArray.Length - 1], typeof(BorderDirEnum));
                }
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }

        private void SetColor(string sAtt, Color newColor)
        {
            if (newColor != this.GetColor(sAtt))
            {
                this._style.SetAttributeIfNeeded(sAtt, new SolidBrush(newColor));
            }
        }

        public Color BackColor
        {
            get
            {
                return this.GetColor("BackColor");
            }
            set
            {
                this.SetColor("BackColor", value);
            }
        }

        internal Brush BackColorBrush
        {
            get
            {
                return (this._style.GetAttribute("BackColor") as Brush);
            }
        }

        public CellBorder Border
        {
            get
            {
                return new CellBorder(this._style);
            }
        }

        internal Brush BorderBrush
        {
            get
            {
                C1.Win.C1FlexGrid.Util.Styles.Border attribute = (C1.Win.C1FlexGrid.Util.Styles.Border) this._style.GetAttribute("Border");
                return attribute._brush;
            }
        }

        public StyleElementFlags DefinedElements
        {
            get
            {
                StyleElementFlags none = StyleElementFlags.None;
                if (this._style.HasAttribute("Font"))
                {
                    none |= StyleElementFlags.Font;
                }
                if (this._style.HasAttribute("BackColor"))
                {
                    none |= StyleElementFlags.BackColor;
                }
                if (this._style.HasAttribute("ForeColor"))
                {
                    none |= StyleElementFlags.ForeColor;
                }
                if (this._style.HasAttribute("Padding"))
                {
                    none |= StyleElementFlags.Margins;
                }
                if (this._style.HasAttribute("Border"))
                {
                    none |= StyleElementFlags.Border;
                }
                if (this._style.HasAttribute("AlignHorz"))
                {
                    none |= StyleElementFlags.TextAlign;
                }
                if (this._style.HasAttribute("AlignVert"))
                {
                    none |= StyleElementFlags.TextAlign;
                }
                if (this._style.HasAttribute("TextEffect"))
                {
                    none |= StyleElementFlags.TextEffect;
                }
                if (this._style.HasAttribute("AlignImage"))
                {
                    none |= StyleElementFlags.ImageAlign;
                }
                if (this._style.HasAttribute("Spacing"))
                {
                    none |= StyleElementFlags.ImageSpacing;
                }
                if (this._style.HasAttribute("Trimming"))
                {
                    none |= StyleElementFlags.Trimming;
                }
                if (this._style.HasAttribute("Wrap"))
                {
                    none |= StyleElementFlags.WordWrap;
                }
                return none;
            }
            set
            {
                this.Clear(~value);
            }
        }

        public System.Drawing.Font Font
        {
            get
            {
                return (System.Drawing.Font) this._style.GetAttribute("Font");
            }
            set
            {
                this._style.SetAttributeIfNeeded("Font", value);
            }
        }

        public Color ForeColor
        {
            get
            {
                return this.GetColor("ForeColor");
            }
            set
            {
                this.SetColor("ForeColor", value);
            }
        }

        [DefaultValue(1)]
        public ImageAlignEnum ImageAlign
        {
            get
            {
                return (ImageAlignEnum) ((int) this._style.GetAttribute("AlignImage"));
            }
            set
            {
                this._style.SetAttributeIfNeeded("AlignImage", (AlignImageEnum) value);
            }
        }

        [DefaultValue(2)]
        public int ImageSpacing
        {
            get
            {
                return (int) this._style.GetAttribute("Spacing");
            }
            set
            {
                this._style.SetAttributeIfNeeded("Spacing", value);
            }
        }

        public C1.Win.C1FlexGrid.Util.Margins Margins
        {
            get
            {
                return (C1.Win.C1FlexGrid.Util.Margins) this._style.GetAttribute("Padding");
            }
            set
            {
                this._style.SetAttributeIfNeeded("Padding", value);
            }
        }

        public string Name
        {
            get
            {
                return this._style.GetName();
            }
            set
            {
                if (!CellStyleCollection.NameIsValid(value) || !this._style.SetName(value))
                {
                    throw new ArgumentException("Invalid or duplicate style names.");
                }
            }
        }

        [DefaultValue(10)]
        public TextAlignEnum TextAlign
        {
            get
            {
                AlignHorzEnum attribute = (AlignHorzEnum) this._style.GetAttribute("AlignHorz");
                AlignVertEnum enum3 = (AlignVertEnum) this._style.GetAttribute("AlignVert");
                for (int i = 0; i < _taMap.Length; i++)
                {
                    if ((_taMap[i]._ah == attribute) && (_taMap[i]._av == enum3))
                    {
                        return _taMap[i]._ta;
                    }
                }
                return TextAlignEnum.GeneralCenter;
            }
            set
            {
                for (int i = 0; i < _taMap.Length; i++)
                {
                    if (_taMap[i]._ta == value)
                    {
                        this._style.SetAttributeIfNeeded("AlignHorz", _taMap[i]._ah);
                        this._style.SetAttributeIfNeeded("AlignVert", _taMap[i]._av);
                        return;
                    }
                }
            }
        }

        [DefaultValue(0)]
        public C1.Win.C1FlexGrid.TextEffectEnum TextEffect
        {
            get
            {
                return (C1.Win.C1FlexGrid.TextEffectEnum) ((int) this._style.GetAttribute("TextEffect"));
            }
            set
            {
                this._style.SetAttributeIfNeeded("TextEffect", (C1.Win.C1FlexGrid.Util.Styles.TextEffectEnum) value);
            }
        }

        [DefaultValue(4)]
        public StringTrimming Trimming
        {
            get
            {
                return (StringTrimming) this._style.GetAttribute("Trimming");
            }
            set
            {
                this._style.SetAttributeIfNeeded("Trimming", value);
            }
        }

        [DefaultValue(false)]
        public bool WordWrap
        {
            get
            {
                return (bool) this._style.GetAttribute("Wrap");
            }
            set
            {
                this._style.SetAttributeIfNeeded("Wrap", value);
            }
        }

        [StructLayout(LayoutKind.Sequential)]
        private struct TextAlignMap
        {
            internal TextAlignEnum _ta;
            internal AlignHorzEnum _ah;
            internal AlignVertEnum _av;
            internal TextAlignMap(TextAlignEnum ta, AlignHorzEnum ah, AlignVertEnum av)
            {
                this._ta = ta;
                this._ah = ah;
                this._av = av;
            }
        }
    }
}

